/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package crypto;


import java.sql.SQLException;
import java.util.Calendar;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.lang.String;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Cecilia Bartley, Isaac Osesina
 */

public class Results {

    // takes all words from a file, scrambles them and puts the output into the set called newSet

public Set<String> ScrambleEverything (Set<String> finaloutput){
    Set<String> ScrambleRecursion = null;
    Set<String> newSet = new HashSet<String>();
    Word_Match wordmatchtest = new Word_Match();

    for (String element:finaloutput)//for every element of the string set
    {
            ScrambleRecursion = wordmatchtest.ScrambleRecursion(element);//use the scramble recursion method

            newSet.addAll(ScrambleRecursion);//add the new word to the new set called newSet
    }
    return newSet; //return the new set called newSet
}


    //checks each element line by line from a set against 1 line of text

public Set<String> TextWordCheck (String text, Set<String> word){
    Set<String> newSet2 = new HashSet<String>(); //newSet2 object
    Word_Match wordmatchtest = new Word_Match();  //new Word_Match object called wordmatchtest

      for (String element2:word)
         {
             boolean res = wordmatchtest.cc(text,element2);

             if(res == true)

                newSet2.add(element2);

         }
         return newSet2;

}
//Return a map of type string and integer array list the following method whose parameters are (string txt and string set called search)
public Map<String,ArrayList<Integer>> SearchandIndex (String text, Set<String> search){
    Map<String,ArrayList<Integer>> newSet3 = new HashMap();  //Creates a new hashmap object called newSet3
    Word_Match wordmatchtest = new Word_Match();  //Creates a new object call

    for (String element3:search) //for each element within the set
    {
            ArrayList<Integer> cc2 = wordmatchtest.cc2(text, element3);  //Array list of type integer called cc2 equals

            if(cc2 != null && !cc2.isEmpty())  //Nested loop, that if the array list is not of type null or empty, then add

                newSet3.put(element3, cc2);

    }
        return newSet3;
}


public Map MM (ArrayList<String> tweets, HashSet<String> targetwords){
    //get tweets and target words, scramble the target words, check them against the arraylist of tweets and save the results in a map
   HashSet<String> tgwrds = new HashSet();
   Set<String> checklist = new HashSet<String>();
    Map map1 = new HashMap();
    Word_Match  scramble = new Word_Match();

    //IO: It is not necessary to write another way of scrambling the target words when ScrambleEverything(Set<String> finaloutput)
    //already exist. It is easier to just use:
    //Set everthingAlreadyScrambled = ScrambleEverything(targetwords);

    //IO-2: There is no need to go through the targetwords indivudually because ScrambleEverything(Set<String> finaloutput) can
    //already do that and return all of the possible orders.
    //All that's left to be done is to compare everthingAlreadyScrambled to the words in whatever data set is being analyzed
    
    for (String element4: targetwords)  // for each element of the string hashset parameter targetwords
    {

            //Set<String> SR = scramble.ScrambleRecursion(element4);  //scramble each element and store the values in set of type string in variable scramble recursion
            Set<String> ScrambleEverything = this.ScrambleEverything(element4);

            Set<String> SR = scramble.ScrambleRecursion(element4);  //scramble each element and store the values in set of type string in variable scramble recursion
    }
            //IO: the variable SR is not defined that's why it indicates an error. in short SR does not exist so far.
            if (SR != null && !SR.isEmpty()){  //if scramble recursion isn't empty or null

            //IO-2: ScrambleEverything does not exist here. It only exist between lines 99 and 101 i.e. within the for loop braces
            tgwrds.addAll(ScrambleEverything); //adds the scrambled word to the new hash set called tgwrds
            }
             if (tgwrds != null && !tgwrds .isEmpty()){  //if scramble recursion isn't empty or null

                    for (String element5: tweets) // for every element of the array list of type string parameter tweets
                    {
                       //boolean checker = scramble.cc(element5, SR);  // check if each scrambled word exists within the tweet
                       this.TextWordCheck(element5, element4);

                            if(checklist != null && !checklist .isEmpty()){

                                this.SearchandIndex(element5, checklist);
                            }
                                  //if (checker  == true){

                                  // ArrayList<Integer> cc2 = scramble.cc2(element5, SR);    //if the word exists, return the search word and the index of the word within the tweet

                                  // map1.put(cc2, SR);
                            }

                    }

            }
//return map1;
}

public static void main(String [] args) {

        //Read the target word file
        HashSet<String> targetwords;
        FileRead fr = new FileRead();
        Set main = fr.main(args);


        //Read the database
        ArrayList<String> tweets;

       // DBQuery dbquerytest = new DBQuery("jdbc:mysql://144.167.34.111:3306/", "twitterdatacopy", "cecilia", "cecilia123", Calendar.getInstance());
       // List<Tweet> queryTweetTextAndLocation = dbquerytest.queryTweetTextAndLocation("");


        Results r = new Results();
        Map MM = r.MM(tweets, targetwords);

     //creates a word match object
//        Word_Match wordmatchtest = new Word_Match();
//       1 wordmatchtest.ScrambleRecursion(""); //scrambles the word into textlength! possible combinations of the characters
//       2 wordmatchtest.cc("Why are you getting frustrated Isaac?", "are"); //takes a string and tests to see if the word appears within the string
//       3 wordmatchtest.cc2("Cecilia is a UALR students ", "s ");
//


    }
}




